import apiSendMobileCode from '../../../../../yali-sdk/business/live/api/apiSendMobileCode';
import apiBandMobileCode from '../../../../../yali-sdk/business/live/api/apiBandMobileCode';
import apiUpdataMobile from '../../../../../yali-sdk/business/live/api/apiUpdataMobile';
import { SUCCESS } from '../../../../../yali-sdk/utils/success';
import { FAIL } from '../../../../../yali-sdk/utils/fail';
import { getExtConfig } from '../../../../../utils/checkAuth';
import getIn from '../../../../../yali-sdk/utils/getIn';

let timer = '';
Page({
    data: {
        num: 0,
        is_send_code: false,
        is_auth: false,
        count_down: 0,
        submit: {},
        uid: 0,
    },
    onLoad(options) {
        const self = this;
        self.setData({ uid: options.uid ? options.uid : '' });
    },
    onUnload() {
    },
    bandMobile(e) {
        const phone = e.detail.value;
        phone ? this.setData({ 'submit.phone': e.detail.value, is_send_code: true}) : this.setData({ 'submit.phone': '',is_send_code: false });
    },
    bandMobileCode(e) {
        const self = this;
        self.setData({
            'submit.mobile_code': e.detail.value,
            is_auth: true,
        });
    },
    sendMobileCode(e) {
        try {
            const self = this;
                wx.showLoading();
                if (!(/^1[34578]\d{9}$/.test(self.data.submit.phone))) {
                    wx.hideLoading();
                    wx.showModal({
                        content: '手机号码格式错误' });
                    return;
                }
                global.actionDispatcher.execute(apiSendMobileCode, {
                    mobile: self.data.submit.phone,
                }, (f, fs) => {
                    if (f === SUCCESS) {
                        const _this = this;
                        if (_this.data.count_down < 1) {
                            clearInterval(timer);
                            _this.data.count_down = 91;
                            timer = setInterval(() => {
                                const now_count = _this.data.count_down - 1;
                                if (now_count != -1) {
                                    _this.setData({ count_down: now_count });
                                } else {
                                    clearInterval(timer);
                                }
                            }, 1000);
                        }
                        wx.hideLoading();
                        wx.showToast({
                            title: '验证码发送成功',
                            icon: 'none',
                            duration: 1500,
                        });
                    } else if (f === FAIL) {
                        wx.hideLoading();
                        wx.showToast({
                            title: '验证码发送失败',
                            icon: 'none',
                            duration: 1500,
                        });
                        return false;
                    }
                });
            // }
        } catch (e) {
            wx.hideLoading();
            wx.showToast({ title: '验证CODE异常', icon: 'none', duration: 1500 });
        }
    },
    is_token(e) {
        console.log(e,'fffffffffffff');
        wx.showLoading();
        const self = this;
        console.log(self,'self');
        if (!self.data.submit.phone) {
            wx.hideLoading();
            wx.showModal({ content: '请填写手机号码' });
            return;
        }
        if(!self.data.submit.mobile_code){
             wx.hideLoading();
            wx.showModal({ content: '请填写验证码' });
            return
            }
            const mobile =self.data.submit.phone ;
            const url = '/pages/anchor/center/seting/seting?mobile='+ mobile;
            console.log(url,'url');
            const param = {
                code: self.data.submit.mobile_code,
                mobile: mobile,
                uid:self.options.uid
            }; 
          global.actionDispatcher.execute(apiUpdataMobile, param, (f, fs) => {
                     if (f === SUCCESS) {
                             wx.hideLoading();
                             wx.showToast({ title: '绑定成功',
                                            icon: 'none',
                                            duration: 1500,
                                         });
                                        wx.navigateBack({ url });
                                    } else if (f === FAIL) {
                                  wx.hideLoading();
                               wx.showToast({ title: fs.msg, icon: 'none', duration: 1500 });
                       }
             });
                   
    },
});
